class GenerateMatrix_59 {
    public int[][] generateMatrix(int n) {
        int[][] nums = new int[n][n];
        int startx = 0;
        int starty = 0;
        int offset = 1;
        int count = 1;
        int loop = 1;
        int i, j;
        while (loop <= n / 2) {
            for (j = starty; j < n - offset; j++) {
                nums[startx][j] = count++;
            }
            for (i = startx; i < n - offset; i++) {
                nums[i][j] = count++;
            }
            for (; j > starty; j--) {
                nums[i][j] = count++;
            }
            for (; i > startx; i--) {
                nums[i][j] = count++;
            }
            startx++;
            starty++;
            offset++;
            loop++;
        }
        if (n % 2 == 1) {
            nums[startx][starty] = count;
        }
        return nums;
    }
}
